﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CloudFranceTriple.Models;
using WebMatrix.WebData;

namespace CloudFranceTriple.WebRole.Areas.Gaming.Controllers
{
    [CloudFranceTriple.WebRole.Filters.InitializeSimpleMembership]
    public class GameController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        private readonly IWebSecurityWrapper _webSecurity;
        public GameController()
        {
            this._webSecurity = new WebSecurityWrapper();
        }

        public GameController(IWebSecurityWrapper webSecurity)
        {
            this._webSecurity = webSecurity;
        }

        //
        // GET: /Gaming/Game/
        [AllowAnonymous]
        [Authorize(Roles = "RoleAdminGame")]
        [ChildActionOnly]
        public ActionResult PLayerListByGame(int Id = 0)
        {
            var playersId = db.UserProfile.Join(db.PlayerOnGameSet.Where(game => game.GameId == Id), user => user.UserId, player => player.UserId, (user, player) => player.UserId);
            var players = db.UserProfile.Join(db.PlayerOnGameSet.Where(game => game.GameId == Id), user => user.UserId, player => player.UserId, (user, player) => (player.PlayerNickName + " (" + user.UserName + ")")).OrderBy(pl => pl);
            String result = String.Empty;

            foreach (String name in players)
                result += name + "; ";

            ViewBag.Game_Id = Id;
            ViewBag.IsSignedUp = (User.Identity.Name != String.Empty) && playersId.Contains(_webSecurity.CurrentUserId);

            return PartialView("IndexShowPlayerByGame", result);
        }

        [AllowAnonymous]
        [Authorize(Roles = "RoleAdminGame")]
        [ChildActionOnly]
        public ActionResult ShowGame(int Id = 0)
        {
            var gameset = db.GameSet.Where(game2 => game2.GameGroupSet_Id == Id).OrderBy(game => game.OrderGame).Include(g => g.GameGroupSet).Include(g => g.PublisherSet);
            return PartialView("IndexShowGameByGroup", gameset.ToList());
        }

        [AllowAnonymous]
        [Authorize(Roles = "RoleAdminGame")]
        public ActionResult Index()
        {
            var gamegroupset = db.GameGroupSet.OrderBy(gg => gg.Order);
            return View(gamegroupset.ToList());
        }

        [AllowAnonymous()]
        [Authorize(Roles = "RoleAdminGame")]
        public ActionResult Details(int id = 0)
        {
            var gameset = db.GameSet.Find(id);
            if (gameset == null)
            {
                return HttpNotFound();
            }
            return View(gameset);
        }

        //
        // GET: /Gaming/Game/Create

        [Authorize(Roles = "RoleAdminGame")]
        public ActionResult Create()
        {
            ViewBag.GameGroupSet_Id = new SelectList(db.GameGroupSet, "Id", "Label");
            ViewBag.Publisher_Id = new SelectList(db.PublisherSet, "Id", "NamePublisher");
            return View(new GameSet { OrderGame = 10 });
        }

        //
        // POST: /Gaming/Game/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminGame")]
        public ActionResult Create(GameSet gameset)
        {
            if (ModelState.IsValid)
            {
                db.GameSet.Add(gameset);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.GameGroupSet_Id = new SelectList(db.GameGroupSet, "Id", "Label", gameset.GameGroupSet_Id);
            ViewBag.Publisher_Id = new SelectList(db.PublisherSet, "Id", "NamePublisher", gameset.Publisher_Id);
            return View(gameset);
        }

        //
        // GET: /Gaming/Game/Edit/5

        [Authorize(Roles = "RoleAdminGame")]
        public ActionResult Edit(int id = 0)
        {
            var gameset = db.GameSet.Find(id);
            if (gameset == null)
            {
                return HttpNotFound();
            }
            ViewBag.GameGroupSet_Id = new SelectList(db.GameGroupSet, "Id", "Label", gameset.GameGroupSet_Id);
            ViewBag.Publisher_Id = new SelectList(db.PublisherSet, "Id", "NamePublisher", gameset.Publisher_Id);
            return View(gameset);
        }

        //
        // POST: /Gaming/Game/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminGame")]
        public ActionResult Edit(GameSet gameset)
        {
            if (ModelState.IsValid)
            {
                db.Entry(gameset).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GameGroupSet_Id = new SelectList(db.GameGroupSet, "Id", "Label", gameset.GameGroupSet_Id);
            ViewBag.Publisher_Id = new SelectList(db.PublisherSet, "Id", "NamePublisher", gameset.Publisher_Id);
            return View(gameset);
        }

        //
        // GET: /Gaming/Game/Delete/5

        [Authorize(Roles = "RoleAdminGame")]
        public ActionResult Delete(int id = 0)
        {
            GameSet gameset = db.GameSet.Find(id);
            if (gameset == null)
            {
                return HttpNotFound();
            }
            return View(gameset);
        }

        //
        // POST: /Gaming/Game/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminGame")]
        public ActionResult DeleteConfirmed(int id)
        {
            GameSet gameset = db.GameSet.Find(id);
            db.GameSet.Remove(gameset);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}